package com.hnfnu.mapper;

import com.hnfnu.entity.Project;
import com.hnfnu.protocol.dto.ProjectPurposeNameDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ProjectMapper {

    /**
     * 添加Project
     *
     * @param project 实例对象
     * @return 影响行数
     */
    @Insert("insert into project (name, background, target, theme, sponsor, \n" +
            "                     content, img,gmt_create, gmt_modified, purpose_detail_id, goal)\n" +
            "values (#{name}, #{background}, #{target}, #{theme}, #{sponsor}, \n" +
            "#{content}, #{img},#{gmtCreate}, #{gmtModified}, #{purposeDetailId}, #{goal})")
    int insert(Project project);

    /**
     * 删除Project
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Integer id);

    /**
     * 查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("select * from project where id = #{id}")
    Project getById(Integer id);

    /**
     * 查询全部数据
     * 分页使用MyBatis的插件实现
     *
     * @return 对象列表
     */
    List<Project> listProjects();

    /**
     * 实体作为筛选条件查询数据
     *
     * @param project 实例对象
     * @return 对象列表
     */
    List<Project> listProjects(Project project);

    /**
     * 修改Project，根据 project 的主键修改数据
     *
     * @param project
     * @return 影响行数
     */
    int update(Project project);

    @Select("select name from project where id = #{id}")
    String getNameById(Integer id);

    @Select("SELECT p.id, p.name, p.background, p.target,\n" +
            "     p.theme,p.sponsor,p.content,p.img, p.gmt_create as gmtCreate, p.gmt_modified as gmtModified,\n" +
            "     p.goal,  p2.name as purposeName\n" +
            "FROM project p\n" +
            "JOIN purpose_detail pd ON p.purpose_detail_id = pd.id\n" +
            "JOIN purpose p2 on pd.purpose_id = p2.id;")
    List<ProjectPurposeNameDTO> getProjectPurposeId();

    @Select("select name as purposeDetailName from purpose_detail where id = #{purposeDetailId}")
    String getPurposeDetailNameByDetailId(Integer purposeDetailId);

    @Select("select goal from project where id = #{projectId};")
    Double getGoalById(Integer projectId);
    @Select("SELECT count(*)\n" +
            "FROM project\n" +
            "WHERE YEARWEEK(gmt_create, 1) = YEARWEEK(CURDATE(), 1);")
    int getWeekProjectNum();
}